package jmd.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Vector;

import jmd.bean.ClientEntity;

public class DataIO
{

	File file = new File("D:\\datacst.txt");

	/**
	 * This method is used to store one record to the file .
	 * 
	 * @param entity
	 * @throws IOException
	 */
	public void storeOneRecord(ClientEntity entity) throws IOException
	{
		// to cover the file content

		RandomAccessFile rf = new RandomAccessFile(file, "rw");

		System.out.println("the length is " + rf.length());
		String data = entity.getIp() + " " + entity.getFileName() + " "
				+ entity.getType() + " " + entity.getColor() + " " + entity.isFillOut();
		rf.seek(rf.length());

		rf.write((data + "\r\n").getBytes());
		System.out.println("the length is " + rf.length());
		rf.close();
	}

	/**
	 * Store the the record in the vector to the file
	 * 
	 * @param argAllEntity
	 * @throws IOException
	 */
	public void storeAllData(Vector<ClientEntity> argAllEntity)
			throws IOException
	{
		if (file.exists())
		{
			file.delete();
		} else if (!file.exists())
		{
			file.createNewFile();
		}
		for (int i = 0; i < argAllEntity.size(); i++)
		{
			storeOneRecord(argAllEntity.get(i));
		}
	}

	/**
	 * To get all the record from the file , if the ip is matched, it will return
	 * a object .
	 * 
	 * @return
	 * @throws IOException
	 */
	public ClientEntity getClientData(String argIp) throws IOException
	{
		ClientEntity bm = null;

		if (!file.exists())
		{
			file.createNewFile();
		}

		FileReader fr = new FileReader(file);

		BufferedReader br = new BufferedReader(fr, 8);

		String line = null;

		while (br.ready())
		{
			line = br.readLine();
			String a[] = line.split(" ");
			if (argIp.equals(a[0].toString()))
			{
				try
				{
					bm = new ClientEntity();
					bm.setIp(a[0].toString());
					bm.setFileName(a[1].toString());
					bm.setType(Integer.parseInt(a[2]));
					bm.setColor(Integer.parseInt(a[3]));
					bm.setFillOut(Boolean.valueOf(a[4]));
				} catch (Exception e)
				{
					bm = new ClientEntity();
				}

			}
		}

		br.close();

		return bm;

	}

	/**
	 * To get all the record from the file , it will return a vector
	 * 
	 * @return
	 * @throws IOException
	 */
	public Vector<ClientEntity> getClientVectorData() throws IOException
	{
		Vector<ClientEntity> clientEntity = new Vector<ClientEntity>();

		ClientEntity bm = null;

		if (!file.exists())
		{
			file.createNewFile();
		}

		FileReader fr = new FileReader(file);

		BufferedReader br = new BufferedReader(fr, 8);

		String line = null;

		while (br.ready())
		{
			line = br.readLine();
			String a[] = line.split(" ");
			try
			{
				bm = new ClientEntity();
				bm.setIp(a[0].toString());
				bm.setFileName(a[1].toString());
				bm.setType(Integer.parseInt(a[2]));
				bm.setColor(Integer.parseInt(a[3]));
				bm.setFillOut(Boolean.valueOf(a[4]));
				clientEntity.add(bm);
			} catch (Exception e)
			{
			}
		}

		br.close();
		return clientEntity;

	}
}
